# -* - coding: UTF-8 -* -
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8')

import threading,string
import time 
from time import sleep
import os
import logging
import json
import pika
from task import *
from base_thread import *
#提交任务到FastCGI
class CommandThread(Thread):  
	def __init__(self,queue):  
		Thread.__init__(self) 
		self.queue = queue
	
	def on_run(self):
		#logging.info("CommandThread Begin")
		try:
			while not self.queue.empty():
				task = self.queue.get()
				logging.info('get task')
				if task:
					self.process_command(task)
		except Exception as e:
			logging.error('CommandThread.on_run')
			logging.exception(e)
			time.sleep(0.1)
		#logging.info("CommandThread End")	


	def on_stop(self):
		logging.info('CommandThread on stop')
		
	def process_command(self,task):
		response = False
		if hasattr(task, task.command):
			func = getattr(task, task.command)		#根据函数名获得函数
			if func:
				try:
					response=func()
				except Exception as e:
					logging.error('process_command')
					logging.exception(e)
					time.sleep(0.1)					
		
		return response		